<?php
	// Load the modules for the plugin.
	include('tweaks/roster/member.php');
	include('tweaks/roster/rosterui.php');
	
	// Tweaker for creating WoW rosters.

	$roster_tweak = new Tweak("Roster Tweak");

	$roster_tweak->author("Adam Dickinson");

	$roster_tweak->description("A WoW Guild Raid/PvP Roster Creator.");

	$roster_tweak->addInterface('roster_show_options');
	$roster_tweak->addTweak('rosters-page', 'roster_display_pages');
	$roster_tweak->addTweak('members-page', 'members_display_pages');
	
	// Main UI for Admin
	function roster_show_options() {
		// Make the roster subdirectory if it doesn't exist.
		if (!is_dir('tweaks/roster')) {
			mkdir('tweaks/roster');
		}
		
		$membersFile = 'tweaks/roster/members.txt';
		$rostersFile = 'tweaks/roster/rosterdata.tdb';
		
		if (file_exists($membersFile)) {
			$members = file_get_contents($membersFile);
			$members = unserialize($members);
		}		

		if (file_exists($rostersFile)) {
			$rosters = file_get_contents($rostersFile);
			$rosters = unserialize($rosters);
		}		
		
		// Admin Display.
		$error = false;		
		
		// Determine which form to display.
		switch($_GET['r']) {
			// Add a new member.
			case 'Add':
				// If something has been submitted, validate and save.
				if (isset($_POST['submit'])) {
					$error = validate_member_data($membersFile, $members, $_POST);
					if (!empty($error)) {
						echo '<p>' . $error . '</p>';
					}
				}
				// Add member UI
				member_ui($_POST);		
				break;
			
			// Modify existing members.
			case 'Modify':
				// Delete the selected member.
				if (isset($_GET['delete'])) {
					// Delete the specified record and save the file.
					if ($_GET['delete'] < count($members)) {
						array_splice($members, $_GET['delete'], 1);
						file_put_contents($membersFile, serialize($members));
					}
					modify_ui($members);
				}
				// Edit the selected member.
				elseif (isset($_GET['edit'])) {
					// Validate the changes.
					if (isset($_POST['submit'])) {
						$error = validate_member_data($membersFile, $members, $_POST);
						if (!empty($error)) {
							echo '<p>' . $error . '</p>';
						}
						member_ui($_POST);
					}
					else {
						// Send the member to the form to be edited
						member_ui($members[$_GET['edit']]);
					}
				}
				// Display the Modify Members UI
				else {
					modify_ui($members);
				}
				break;
				
			// Create a new roster.
			case 'New':
				// Switch case to sort what submit button was pressed
				switch ($_POST['submit']) {
					// Add Members to the list.
					case '<- Add':
						// Check if there is existing records in the array.
						if (!isset($_SESSION['newroster'])) {
							$_SESSION['newroster'] = $_POST['members'];
						}
						else {
							array_splice($_SESSION['newroster'], -1, 0, $_POST['members']);
							// Get rid of duplicate values.
							$_SESSION['newroster'] = array_unique($_SESSION['newroster']);
							// Sort the array.
							sort($_SESSION['newroster']);
						}
						break;
						
					// Delete Members from the list
					case 'Del ->':
						// Check if an array exists in the session array
						if (isset($_SESSION['newroster'])) {
							// Flip the keys of the session array
							$_SESSION['newroster'] = array_flip($_SESSION['newroster']);
							foreach ($_POST['roster'] as $del_noob) {
								// Remove the items to be deleted.
								unset($_SESSION['newroster'][$del_noob]);
							}
							// Flip the keys of the session array again and sort it
							$_SESSION['newroster'] = array_flip($_SESSION['newroster']);
							sort($_SESSION['newroster']);
						}
						break;
						
					// Save the roster
					case 'Save':
						// Validate the roster.
						$error = validate_roster_data($members, $_POST);
						if (!empty($error)) {
							echo '<p>' . $error . '</p>';
						}
						break;
				}
				
				roster_ui($members, $_POST);
				break;
				
			// Modify existing rosters.
			case 'Modify_Roster':
				// Delete the selected roster
				if (isset($_GET['delete'])) {
					unset($rosters[$_GET['delete']]);
					
					// Save the updateds roster list
					$save_roster = serialize($rosters);
					file_put_contents($rostersFile, $save_roster);
					
					// Display the UI
					modify_rosters_ui($rosters);					
				}
				// Modify an existing roster.
				elseif (isset($_GET['edit'])) {
					// Determine what submit button was pressed.
					switch ($_POST['submit']) {
						// Add Members to the list.
						case '<- Add':
							// Check if there is existing records in the array.
							if (!isset($_SESSION['newroster'])) {
								$_SESSION['newroster'] = $_POST['members'];
							}
							else {
								array_splice($_SESSION['newroster'], -1, 0, $_POST['members']);
								// Get rid of duplicate values.
								$_SESSION['newroster'] = array_unique($_SESSION['newroster']);
								// Sort the array.
								sort($_SESSION['newroster']);
							}
							roster_ui($members, $_POST);
							break;
						
						// Delete Members from the list
						case 'Del ->':
							// Check if an array exists in the session array
							if (isset($_SESSION['newroster'])) {
								// Flip the keys of the session array
								$_SESSION['newroster'] = array_flip($_SESSION['newroster']);
								foreach ($_POST['roster'] as $del_noob) {
									// Remove the items to be deleted.
									unset($_SESSION['newroster'][$del_noob]);
								}
								// Flip the keys of the session array again and sort it
								$_SESSION['newroster'] = array_flip($_SESSION['newroster']);
								sort($_SESSION['newroster']);
							}
							roster_ui($members, $_POST);
							break;
						
						// Save the roster
						case 'Save':
							// Validate the roster.
							$error = validate_roster_data($members, $_POST);
							if (!empty($error)) {
								echo '<p>' . $error . '</p>';
							}
							roster_ui($members, $_POST);
							break;
												
						// Default loads the selected roster's members into the session array and loads the roster ui to edit
						default:
							foreach ($rosters[$_GET['edit']]['members'] as $temp_memb) {
								$_SESSION['newroster'][] = $temp_memb['name'];
							}
							roster_ui($members, $rosters[$_GET['edit']]);
							break;
					}
				}
				else {
					modify_rosters_ui($rosters);
				}
				break;
			
			// Display for top level settings.
			default:
				echo '
					<object>
						<ul>
							<li><a href="?tweak=roster-tweak&r=Add">Add Member</a></li>
							<li><a href="?tweak=roster-tweak&r=Modify">Modify Members</a></li>
							<li><a href="?tweak=roster-tweak&r=New">New Roster</a></li>
							<li><a href="?tweak=roster-tweak&r=Modify_Roster">Modify Rosters</a></li>
						</ul>
					</object>';
				break;
		}
	}
	
	// Function gets called from the rosters page.
	function roster_display_pages() {
		$roster_cfg = file_get_contents('data/tweaks/roster/rosterdata.tdb');
		$roster_cfg = unserialize($roster_cfg);
		
		// No Rosters Created yet
		if (count($roster_cfg) == 0) {
			$roster_page = '<p>Welcome to the rosters page, Login and start making your rosters.<br />Written by Adam Dickinson.</p>';
		}
		// Only one roster
		elseif (count($roster_cfg) == 1) {
			$roster_page = draw_roster($roster_cfg[implode('', array_keys($roster_cfg))]);
		}
		// 2 or more
		else {
			$roster_page = '<h2>Rosters</h2>
								<ul>';
								foreach ($roster_cfg as $key=>$roster) {
									$roster_page .= '<li><a href="?page=rosters&display='. $key .'">' . $key . '</a></li>';
								}
									
								$roster_page .= '</ul>';
		}
		
		echo $roster_page;
		
		if (isset($_GET['display'])) {
			echo draw_roster($roster_cfg[$_GET['display']]);
		}
	}
	
	// Function to create the tables for the rosters.
	function draw_roster($roster) {
		$count = 0;
	
		$roster_page = '<h2>' . $roster['name'] . ' - ' . $roster['size'] . ' man</h2>
				<h4>Player Vs ' . $roster['raidtype'] . '</h4>
				<table>
					<tr>
						<th>Core</th>
						<th>Name</th>
						<th>Class</th>
						<th>Level</th>
					</tr>';
				foreach($roster['members'] as $mem) {
					$count++;
					
					// If the count reaches raid size insert a blank rows and another headings row.
					if ($count == $roster['size'] + 1) {
						$roster_page .= '<tr><td>&nbsp;</td></tr>
										<tr class=' . $mem['rclass'] .'>
											<th>Reserves</th>
											<th>Name</th>
											<th>Class</th>
											<th>Level</th>
										</tr>';
					}
					
					$roster_page .= '<tr>
						<td><img src="data/pages/rosters/images/' . generate_avatar_filename($mem) . '" /></td>
						<td>' . $mem['name'] . '</td>
						<td>' . $mem['rclass'] . '</td>
						<td>' . $mem['level'] . '</td>
					</tr>';
				}
		$roster_page .= '</table>';
		
		return $roster_page;
	}
	
	// Function gets called from the members page.
	function members_display_pages() {
		// Get the contents of the members file.
		$membersFile = 'data/tweaks/roster/members.txt';
		
		if (file_exists($membersFile)) {
			$members = file_get_contents($membersFile);
			$members = unserialize($members);
		}
		
		$page = '<h2>Guild Members</h2>
				<table>
					<tr>
						<th></th>
						<th>Name</th>
						<th>Class</th>
						<th>Level</th>
					</tr>';
		// Loop through the members array putting each one on a row.
		foreach ($members as $mem) {
			$page .= '<tr>
						<td><img src="data/pages/rosters/images/' . generate_avatar_filename($mem) . '" /></td>
						<td>' . $mem['name'] . '</td>
						<td>' . $mem['rclass'] . '</td>
						<td>' . $mem['level'] . '</td>
					</tr>';
		}
		$page .= '</table>';
		
		echo $page;
	}
	
	// Function to generate the avatar image name.
	function generate_avatar_filename($mem) {
		$filename = $mem['rclass'];
		
		if ($filename == 'Death Knight') {
			$filename = 'DeathKnight';
		}
		
		// Append the image extension and return it.
		$filename .= '.png';
		
		return $filename;
	}
?>